package ch.zbw.pra.sysventory.ui.gui.panels.settingspanel.subpanels;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import ch.zbw.pra.sysventory.domain.businesslogic.DomainController;
import ch.zbw.pra.sysventory.domain.visualisation.VisuControllProcessor;
import ch.zbw.pra.sysventory.ui.gui.panels.settingspanel.SettingsPanel;
import ch.zbw.pra.sysventory.ui.gui.utilities.GuiLayout;

public class ProcessorSpezPanel extends JPanel implements ActionListener, ChangeListener {

	private static final long serialVersionUID = 4521448655638782755L;
	private JCheckBox prozessorName;
	private JCheckBox prozFamily;
	private JCheckBox anzKerne;
	private JCheckBox clock;
	private VisuControllProcessor visuControllProcessor;
	private int amountOfFlags;

	public ProcessorSpezPanel(VisuControllProcessor visuControllProcessor) {
		this.visuControllProcessor = visuControllProcessor;
		this.amountOfFlags = 0;
		init();
	}

	public void init() {
		this.prozessorName = new JCheckBox("Prozessor-Name", this.visuControllProcessor.isProzessorName());
		this.prozessorName.addActionListener(this);
		this.prozessorName.addChangeListener(this);
		this.prozFamily = new JCheckBox("Prozessor-Familie", this.visuControllProcessor.isProzFamily());
		this.prozFamily.addActionListener(this);
		this.prozFamily.addChangeListener(this);
		this.anzKerne = new JCheckBox("Anzahl Kerne", this.visuControllProcessor.isAnzKerne());
		this.anzKerne.addActionListener(this);
		this.anzKerne.addChangeListener(this);
		this.clock = new JCheckBox("Prozessor-Takt", this.visuControllProcessor.isClock());
		this.clock.addActionListener(this);
		this.clock.addChangeListener(this);
		GridBagLayout gridBagLayout = GuiLayout.getGridBagLayout(this);
		double checkBox = 0.0;
		double lable = 1.0;
		gridBagLayout.columnWeights = new double[] { checkBox, checkBox, checkBox, lable };
		int widthCheckbox = SettingsPanel.WIDTH_CHECKBOX;
		gridBagLayout.columnWidths = new int[] { widthCheckbox, widthCheckbox, widthCheckbox, 0 };
		GridBagConstraints gridBagConstraints = GuiLayout.getGridBagConstraints();
		gridBagConstraints.insets = new Insets(5, 5, 5, 5);
		int x = 0;
		int y = 0;
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, this.prozessorName);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, this.prozFamily);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, this.anzKerne);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, 1, 1, new JLabel());

		y++;
		x = 0;
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, this.clock);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, 1, 1, new JLabel());
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, 1, 1, new JLabel());
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, 1, 1, new JLabel());
	}

	public void setValueToAllCheckBoxes(boolean flag) {
		this.prozessorName.setSelected(flag);
		this.prozFamily.setSelected(flag);
		this.anzKerne.setSelected(flag);
		this.clock.setSelected(flag);
	}

	public void actionPerformed(ActionEvent e) {
		this.amountOfFlags = DomainController.getInstance().getSettingsPanel().countAllSelectedFlags();
		if (e.getSource() == this.prozessorName) {
			this.visuControllProcessor.setProzessorName(this.prozessorName.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.prozFamily) {
			this.visuControllProcessor.setProzFamily(this.prozFamily.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.anzKerne) {
			this.visuControllProcessor.setAnzKerne(this.anzKerne.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.clock) {
			this.visuControllProcessor.setClock(this.clock.isSelected());
			checkCanCrement();
		}
	}

	public void stateChanged(ChangeEvent e) {
		this.amountOfFlags = DomainController.getInstance().getSettingsPanel().countAllSelectedFlags();
		if (e.getSource() == this.prozessorName) {
			this.visuControllProcessor.setProzessorName(this.prozessorName.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.prozFamily) {
			this.visuControllProcessor.setProzFamily(this.prozFamily.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.anzKerne) {
			this.visuControllProcessor.setAnzKerne(this.anzKerne.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.clock) {
			this.visuControllProcessor.setClock(this.clock.isSelected());
			checkCanCrement();
		}
	}

	public void checkCanCrement() {
		if (canCrement()) {
			DomainController.getInstance().getSettingsPanel().setAllEnabledStates(true);
		} else {
			DomainController.getInstance().getSettingsPanel().setAllEnabledStates(false);
		}
	}

	public boolean canCrement() {
		return this.amountOfFlags < 8;
	}

	public boolean getProzessorNameState() {
		return this.prozessorName.isSelected();
	}

	public boolean getProzFamilyState() {
		return this.prozFamily.isSelected();
	}

	public boolean getAnzKerneState() {
		return this.anzKerne.isSelected();
	}

	public boolean getClockState() {
		return this.clock.isSelected();
	}

	public void setAllEnabledStates(boolean beEnabledFlag) {
		if (!this.prozessorName.isSelected()) {
			this.prozessorName.setEnabled(beEnabledFlag);
		}
		if (!this.prozFamily.isSelected()) {
			this.prozFamily.setEnabled(beEnabledFlag);
		}
		if (!this.anzKerne.isSelected()) {
			this.anzKerne.setEnabled(beEnabledFlag);
		}
		if (!this.clock.isSelected()) {
			this.clock.setEnabled(beEnabledFlag);
		}

	}
}
